package com.seari.tunnel.service.impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jframe.mybatis.paginator.domain.PageBounds;
import com.jframe.mybatis.paginator.domain.PageList;
import com.seari.tunnel.entity.Role;
import com.seari.tunnel.entity.User;
import com.seari.tunnel.entity.UserRole;
import com.seari.tunnel.mapper.SecurityRoleMapper;
import com.seari.tunnel.mapper.UserMapper;
import com.seari.tunnel.service.IUserService;



@Service(IUserService.SERVICE_NAME)
public class UserServiceImpl implements IUserService {
	
	@Autowired
	UserMapper userMapper;
	
	@Autowired
	SecurityRoleMapper securityRoleMapper;

	@Override
	public int countAll() {

		return userMapper.countAll();
	}

	@Override
	public PageList<User> findAll() {
		
		return userMapper.findAll();
	}

	@Override
	public PageList<User> findPaging(User user,PageBounds rb) {
		
		return userMapper.findPaging(user,rb);
	}

	@Override
	public User getUserByName(String username) {
		
		return userMapper.getUserByName(username);
	}
	@Override
	public User getUserById(long id){
		
		return userMapper.getUserById(id);
	}
	
	
	@Override
	public void createUser(User user){
	    
		userMapper.createUser(user);
	}

	@Override
	public void deleteUser(long id) {
		
		 userMapper.deleteUserById(id);
		 
		 userMapper.deleteUserRoleByUserId(id);
	}

	@Override
	public void deleteUsers(String[] ids) {
		
		userMapper.deleteUsers(ids);
		
	}

    @Override
    public boolean userNameIsExist(User user) {
        
        return userMapper.userNameIsExist(user)>0;
    }

    @Override
    public void updateUser(User user) {
         
        userMapper.updateUser(user);
    }

	@Override
	public void updateUserPassword(User user) {
		 
		userMapper.updateUserPassword(user);
	}

	@Override
	public Set<String> findRoleByUserName(String userName) {
		
		return userMapper.findRoleByUserName(userName);
	}

	@Override
	public List<Role> findSecurityRole() {
		
		return securityRoleMapper.findSecurityRole();
	}

	@Override
	public void addSecurityRole(Role role) {
		securityRoleMapper.addSecurityRole(role);
		
	}

	@Override
	public void updateSecurityRole(Role role) {
		securityRoleMapper.updateSecurityRole(role);
		
	}

	@Override
	public void deleteSecurityRole(int id) {
		securityRoleMapper.deleteSecurityRole(id);
		
	}

	@Override
	public List<User> findSecurityUser() {
		
		return userMapper.findSecurityUser();
	}

	@Override
	public void addSecurityUser(User user) {
		userMapper.addSecurityUser(user);
		int user_id = user.getId().intValue();
		int role_id = Integer.parseInt(user.getRole());
		UserRole userRole = new UserRole();
		userRole.setUserId(user_id);
		userRole.setRoleId(role_id);
		securityRoleMapper.addSecurityUserRole(userRole);
		
	}

	@Override
	public void deleteSecurityUserById(int id) {
		userMapper.deleteUserById(id);
		
	}

	@Override
	public Role getUserRole(long id) {
		// TODO Auto-generated method stub
		return userMapper.getUserRole(id);
	}

}
